using System.Collections.Generic;
using UnityEngine;

public class Example1_Prefab : MonoBehaviour
{
    public GameObject[] gos;
    private Dictionary<int, List<GameObject>> dic = new Dictionary<int, List<GameObject>>();

    public void LoadAssets(int index)
    {
        if (index < 0 || index >= gos.Length)
        {
            return;
        }
        
        GameObject go = gos[index];
        go = GameObject.Instantiate(go);
        float x = Random.Range(0.0f, 100.0f);
        float z = Random.Range(0.0f, 100.0f);

        go.transform.position = new Vector3(x, 0, z);
        
        if (!dic.ContainsKey(index))
        {
            dic.Add(index,new List<GameObject>());
        }

        dic[index].Add(go);

        // PrefabEntityRoot per = go.GetComponent<PrefabEntityRoot>(); //!!!!!
        // per.RefreshTransform();
    }

    public void UnLoadAssets(int index)
    {
        if (index < 0 || index >= gos.Length)
        {
            return;
        }
        
        if (dic.TryGetValue(index, out var lstGo))
        {
            if (lstGo.Count > 0)
            {
                GameObject go = lstGo[^1];
                
                // PrefabEntityRoot per = go.GetComponent<PrefabEntityRoot>(); //!!!!!!
                // WorldController.Instance().RemoveDynamicEntity(per); //!!!!!!
                
                GameObject.Destroy(go);
                lstGo.RemoveAt(lstGo.Count - 1);
            }
        }
    }
}
